package com.aco.party.web.controller.system;

import com.aco.party.model.po.Permission;
import com.aco.party.service.system.PermissionService;
import com.aco.party.web.command.system.EditPermissionCommand;
import com.aco.party.web.validator.system.EditPermissionValidator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Controller
public class ManagePermissionsController extends BaseController {
	private EditPermissionValidator permissionValidator=new EditPermissionValidator();


	@Resource
	private PermissionService permissionService;
	
	/**
	 * 查找所有的权限
	 * @param model
	 */
	@RequestMapping(value="/system/managePermissions")
	@RequiresPermissions(value="user:manage")
	public void managePermissions(Model model){
		model.addAttribute("permissions",permissionService.findAllPermissions());
	}
	
	
	/**
	 * 编辑权限前的展示
	 * @param permissionId
	 * @param command
	 * @return
	 */
	@RequestMapping(value="/system/editPermission",method=RequestMethod.GET)
	@RequiresPermissions(value="user:manage")
	public String showEditPermissionForm(@RequestParam Long permissionId,@ModelAttribute EditPermissionCommand command){
		Permission permission=permissionService.getPermission(permissionId);
		command.setId(permissionId);
		command.setRealm(permission.getRealm());
		return "system/editPermission";
	}
	
	/**
	 * 保存修改后的数据
	 * @param permissionId
	 * @param command
	 * @param errors
	 * @return
	 */
	@RequestMapping(value="/system/editPermission",method=RequestMethod.POST)
	@RequiresPermissions(value="user:manage")
	public String editPermission(@RequestParam Long permissionId,@ModelAttribute EditPermissionCommand command,BindingResult errors){
		//验证数据
		permissionValidator.validate(command, errors);
		if(errors.hasErrors()){
			return "system/editPermission";
		}
		Permission permission=new Permission();
		permission.setId(permissionId);
		permission.setRealm(command.getRealm());
		permissionService.updatePermission(permission);
		return "redirect:/system/managePermissions";
	}
	/**
	 * 显示新增权限页面
	 * @return 转到编辑页面
	 */
	@RequestMapping(value="/system/addPermission",method=RequestMethod.GET)
	@RequiresPermissions("user:manage")
	public String showAddPermissionForm(@ModelAttribute  EditPermissionCommand command){
		
		return "system/editPermission";
	}
	
	/**
	 * 新增权限
	 * @return 权限管理页面
	 */
	@RequestMapping(value="/system/addPermission",method=RequestMethod.POST)
	@RequiresPermissions("user:manage")
	public String addPermission(@ModelAttribute EditPermissionCommand command,BindingResult errors){
		permissionValidator.validate(command, errors);
		if(errors.hasErrors())
			return "system/editPermission";
		Permission permission=new Permission();
		permission.setRealm(command.getRealm());
		permissionService.addPermission(permission);
		return "redirect:/system/managePermissions";
	}
	
	
	/**
	 * 删除权限
	 * @param permissionId
	 * @return
	 */
	@RequestMapping("/system/deletePermission")
	@RequiresPermissions(value="user:manage")
	public String deletePermission(@RequestParam Long permissionId){
		permissionService.deletePermission(permissionId);
		return "redirect:/system/managePermissions";
	}

}
